1520C - Not Adjacent Matrix - CodeForces Solution


constructive algorithms *1000

Please click on ads to support us..

Python Code:

t=int(input())
for i in range(t):
    n=int(input())
    if n==2 :
        print(-1)
    else:    
     cnt=1
     for j in range(n):
         for q in range(n):
             if cnt<=n*n :
              print(cnt,end=' ')
              cnt+=2
             else:
              cnt=2
              print(cnt, end=' ')
              cnt+=2
         print(" ")  

C++ Code:

#include <bits/stdc++.h>
using namespace std;
int main () {
    ios_base :: sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        if(n==2) cout<<-1<<endl;
        else{
            for(int i=1; i<=n*n; i+=2) cout<<i<<' ';//1...9->1,3,5,7,9
            for(int i=2; i<=n*n; i+=2) cout<<i<<' ';//2...9->2,4,6,8
            cout<<endl;
        }
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

727A - Transformation from A to B
822B - Crossword solving
1623A - Robot Cleaner
884B - Japanese Crosswords Strike Back
862B - Mahmoud and Ehab and the bipartiteness
429A - Xor-tree
1675C - Detective Task
950A - Left-handers Right-handers and Ambidexters
672B - Different is Good
1C - Ancient Berland Circus
721A - One-dimensional Japanese Crossword
1715B - Beautiful Array
60B - Serial Time
453A - Little Pony and Expected Maximum
1715A - Crossmarket
1715C - Monoblock
1512C - A-B Palindrome
1679B - Stone Age Problem
402A - Nuts
792A - New Bus Route
221A - Little Elephant and Function
492C - Vanya and Exams
1369B - AccurateLee
892B - Wrath
999A - Mishka and Contest
727C - Guess the Array
1625C - Road Optimization
1715D - 2+ doors
267A - Subtractions
1582A - Luntik and Concerts